import AutoChart from '@/components/Echarts/AutoChart';
// @ts-ignore
import { medicineGrass } from '@/services/api';
import React, { useEffect } from 'react';
import { useImmer } from 'use-immer';

const ColumnCharts: React.FC = () => {
  const [options, setOptions] = useImmer({
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'cross',
        label: {
          backgroundColor: '#6a7985',
        },
      },
    },
    // 图例组件
    legend: {
      show: false,
      textStyle: {
        color: '#fff',
      },
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      containLabel: true,
    },
    xAxis: [
      {
        type: 'category',
        boundaryGap: false,
        data: [],
      },
    ],
    yAxis: [
      {
        type: 'value',
      },
    ],
    series: [
      {
        color: '#8e9dff',
        type: 'line',
        smooth: true,
        stack: 'Total',
        areaStyle: {
          color: {
            type: 'linear',
            x: 0,
            y: 0,
            x2: 0,
            y2: 1,
            colorStops: [
              {
                offset: 0.25,
                color: '#8e9dff',
              },
              {
                offset: 1,
                color: '#fff',
              },
            ],
          },
        },
        emphasis: {
          focus: 'series',
        },
        data: [],
      },
    ],
  });

  useEffect(() => {
    medicineGrass().then((res) => {
      setOptions((draft) => {
        draft.xAxis[0].data = res.data.map((i: API.ChartsData) => i.name)
        draft.series[0].data = res.data.map((i: API.ChartsData) => Number(i.value))
      });
    });
  }, []);

  return <AutoChart option={options as any}></AutoChart>;
};
export default ColumnCharts;
